package de.xelerity.geolessi.srv.impl.routing.bahn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.xelerity.geolessi.types.verbindung.Linie;
import de.xelerity.helper.StringFunktionen;

public class RouteInfoParsingLine {

	private Log log = LogFactory.getLog(RouteInfoParsingLine.class);

	public enum ParsingLineType {
		HALTESTELLEBEZEICHNER, LINIEINFO, ABZEIT, ABZEITMITDATUM, ANZEIT, ANZEITMITDATUM, GLEIS, VERSPAETUNG, GLEISMITDATUM, UNBEKANNT
	};

	String SContentunparsed;
	ParsingLineType Type;
    
	
	public RouteInfoParsingLine(String S) {
		SContentunparsed = S;
		Type = getLineType();
	}

	private ParsingLineType getLineType() {
		if (StringFunktionen.isEmptyTrimed(SContentunparsed)) {
			return ParsingLineType.UNBEKANNT;
		}

		SContentunparsed = SContentunparsed.replaceAll("\\s+", " ").trim();

		if (isGleisMitDatum(SContentunparsed))
			return ParsingLineType.GLEISMITDATUM;

		if (isAbzeitmitDatum(SContentunparsed))
			return ParsingLineType.ABZEITMITDATUM;

		if (isAnzeitmitDatum(SContentunparsed))
			return ParsingLineType.ANZEITMITDATUM;

		if (isGleis(SContentunparsed))
			return ParsingLineType.GLEIS;

		if (isLinie(SContentunparsed))
			return ParsingLineType.LINIEINFO;

		if (isAbzeit(SContentunparsed))
			return ParsingLineType.ABZEIT;

		if (isAnzeit(SContentunparsed))
			return ParsingLineType.ANZEIT;

		// +0
		if (isVerspaetung(SContentunparsed))
			return ParsingLineType.VERSPAETUNG;

		if (isHalteStelleInfo(SContentunparsed))
			return ParsingLineType.HALTESTELLEBEZEICHNER;

		log.warn("Cannot Parse/Identify line: '" + SContentunparsed + '"');
		return ParsingLineType.UNBEKANNT;
	}

	// Stuttgart Hbf (tief)
	private static boolean isHalteStelleInfo(String S) {
		return S.length() > 2;
	}

	public String getHalteStelleInfo() {
		return SContentunparsed;
	}

	private static boolean isVerspaetung(String S) {
		Pattern regex = Pattern.compile("^[+-]?[0-9][0-9]?[0-9]?(\\s+.*|$)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String getVerspaetung() {
		Pattern regex = Pattern.compile("^([+-]?[0-9][0-9]?[0-9]?)(\\s+.*|$)", Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			return regexMatcher.group(1);
		}
		return "";
	}

	// Gl. 1 am 22.08.12
	private static boolean isGleisMitDatum(String S) {
		Pattern regex = Pattern.compile("^Gl\\.\\s*[0-9a-z]+\\s+am\\s+[0-9]?[0-9]\\.[0-9]?[0-9].[0-9][0-9]+(\\s+|$).*",
				Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String[] getGleisMitDatum() {
		String[] Ret = { "", "" };
		Pattern regex = Pattern.compile(
				"^Gl\\.\\s*([0-9a-z]+)\\s+am\\s+([0-9]?[0-9]\\.[0-9]?[0-9].[0-9][0-9]+)(\\s+|$).*", Pattern.DOTALL
						| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			Ret[0] = regexMatcher.group(1);
			Ret[1] = regexMatcher.group(2);
			return Ret;
		}
		return Ret;
	}

	// Gl. 1s
	private static boolean isGleis(String S) {
		Pattern regex = Pattern.compile("^Gl\\.\\s*[0-9a-z]+(\\s+|$).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String getGleis() {
		Pattern regex = Pattern.compile("^Gl\\.\\s*([0-9a-z]+)(\\s+|$).*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE
				| Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			return regexMatcher.group(1);
		}
		return "";
	}

	private static boolean isLinie(String S) {
		return Linie.isLinienToken(S);
	}

	public Linie getLinie() {
		return Linie.parseLinienInfo(SContentunparsed);
	}

	// ab 12:47 am 22.08.12
	private static boolean isAbzeitmitDatum(String S) {
		Pattern regex = Pattern.compile(
				"^ab\\s+[0-9]?[0-9]:[0-9][0-9]\\s+am\\s+[0-9]?[0-9].[0-9]?[0-9].[0-9][0-9]+(\\s.*|$)", Pattern.DOTALL
						| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String[] getAbzeitmitDatum() {
		String[] Ret = { "", "" };
		Pattern regex = Pattern.compile(
				"^ab\\s+([0-9]?[0-9]:[0-9][0-9])\\s+am\\s+([0-9]?[0-9].[0-9]?[0-9].[0-9][0-9]+)(\\s.*|$)",
				Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			Ret[0] = regexMatcher.group(1);
			Ret[1] = regexMatcher.group(2);
			return Ret;
		}
		return Ret;
	}

	// ab 12:47
	private static boolean isAbzeit(String S) {
		Pattern regex = Pattern.compile("^ab\\s+[0-9]?[0-9]:[0-9][0-9](\\s.*|$)", Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String getAbzeit() {
		Pattern regex = Pattern.compile("^ab\\s+([0-9]?[0-9]:[0-9][0-9])(\\s.*|$)", Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			return regexMatcher.group(1);
		}
		return "";
	}

	// an 12:47 am 22.08.12
	private static boolean isAnzeitmitDatum(String S) {
		Pattern regex = Pattern.compile(
				"^an\\s+[0-9]?[0-9]:[0-9][0-9]\\s+am\\s+[0-9]?[0-9].[0-9]?[0-9].[0-9][0-9]+(\\s.*|$)", Pattern.DOTALL
						| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String[] getAnzeitmitDatum() {
		String[] Ret = { "", "" };
		Pattern regex = Pattern.compile(
				"^an\\s+([0-9]?[0-9]:[0-9][0-9])\\s+am\\s+([0-9]?[0-9].[0-9]?[0-9].[0-9][0-9]+)(\\s.*|$)",
				Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			Ret[0] = regexMatcher.group(1);
			Ret[1] = regexMatcher.group(2);
			return Ret;
		}
		return Ret;
	}

	private static boolean isAnzeit(String S) {
		Pattern regex = Pattern.compile("^an\\s+[0-9]?[0-9]:[0-9][0-9](\\s.*|$)", Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		return regex.matcher(S).find();
	}

	public String getAnzeit() {
		Pattern regex = Pattern.compile("^an\\s+([0-9]?[0-9]:[0-9][0-9])(\\s.*|$)", Pattern.DOTALL
				| Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
		Matcher regexMatcher = regex.matcher(SContentunparsed);
		while (regexMatcher.find()) {
			return regexMatcher.group(1);
		}
		return "";
	}

	public static void main(String[] args) {

		// String[] gleisMitDatum = getGleisMitDatum("Gl. 2 am 12.12.1211");
		//
		// System.out.println(gleisMitDatum[0] + "-" + gleisMitDatum[1]);
	}

}
